package com.jsy.order.controller;

import com.github.pagehelper.PageInfo;
import com.jsy.common.constant.StatusCode;
import com.jsy.common.vo.Result;
import com.jsy.order.pojo.Preferential;
import com.jsy.order.service.PreferentialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/****
 * @Author:jsy
 * @Description:
 * @Date www.jsy.com
 *****/

@RestController
@RequestMapping("/preferential")
@CrossOrigin
public class PreferentialController {

    @Autowired
    private PreferentialService preferentialService;

    /***
     * Preferential分页条件搜索实现
     * @param preferential: 查询条件
     * @param page:当前页
     * @param size:每页显示多少条
     * @return 查询结果
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo<Preferential>> findPage(@RequestBody(required = false) Preferential preferential, @PathVariable  int page, @PathVariable  int size){
        //调用PreferentialService实现分页条件查询Preferential
        PageInfo<Preferential> pageInfo = preferentialService.findPage(preferential, page, size);
        return new Result<PageInfo<Preferential>>("查询成功！", pageInfo);
    }

    /***
     * Preferential分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return 查询结果
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo<Preferential>> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用PreferentialService实现分页查询Preferential
        PageInfo<Preferential> pageInfo = preferentialService.findPage(page, size);
        return new Result<PageInfo<Preferential>>("查询成功！", pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param preferential 查询条件
     * @return 查询结果
     */
    @PostMapping(value = "/search" )
    public Result<List<Preferential>> findList(@RequestBody(required = false) Preferential preferential){
        //调用PreferentialService实现条件查询Preferential
        List<Preferential> list = preferentialService.findList(preferential);
        return new Result<List<Preferential>>("查询成功！", list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id 主键
     * @return 查询结果
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        //调用PreferentialService实现根据主键删除
        preferentialService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功！");
    }

    /***
     * 修改Preferential数据
     * @param preferential 更新对象
     * @return 操作结果
     */
    @PutMapping
    public Result update(@RequestBody Preferential preferential){
        //调用PreferentialService实现修改Preferential
        preferentialService.update(preferential);
        return new Result(true, StatusCode.OK, "修改成功！");
    }

    /***
     * 新增Preferential数据
     * @param preferential 新增对象
     * @return 操作结果
     */
    @PostMapping
    public Result add(@RequestBody Preferential preferential){
        //调用PreferentialService实现添加Preferential
        preferentialService.add(preferential);
        return new Result(true, StatusCode.OK, "添加成功！");
    }

    /***
     * 根据ID查询Preferential数据
     * @param id 主键
     * @return 查询结果
     */
    @GetMapping("/{id}")
    public Result<Preferential> findById(@PathVariable Integer id){
        //调用PreferentialService实现根据主键查询Preferential
        Preferential preferential = preferentialService.findById(id);
        return new Result<Preferential>("查询成功！", preferential);
    }

    /***
     * 查询Preferential全部数据
     * @return 查询结果
     */
    @GetMapping
    public Result<List<Preferential>> findAll(){
        //调用PreferentialService实现查询所有Preferential
        List<Preferential> list = preferentialService.findAll();
        return new Result<List<Preferential>>("查询成功！", list);
    }
}
